package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.*;
import com.ylyq.ylxx.model.request.*;
import com.ylyq.ylxx.service.IOceanengineSiteService;
import com.ylyq.ylxx.util.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName OceanengineSiteServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/27 18:17
 * @Version 1.0
 */
@Service
public class OceanengineSiteServiceImpl implements IOceanengineSiteService {
    @Override
    public ResponseData getLanding(GetLandingRequest getLandingRequest) {

        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "tools/landing_group/get/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getLandingRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineGetLandingPackage oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineGetLandingPackage.class);
        return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
    }
    @Override
    public ResponseData getTetrisSite(TetrisSiteRequest getLandingRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri ="tools/site/get/";

        if(StringUtils.isBlank(getLandingRequest.getStatus())){
            getLandingRequest.setStatus("SITE_ONLINE");//默认查已上线状态的
        }

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());
        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getLandingRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineGetTetrisSite oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineGetTetrisSite.class);
        return new ResponseData(ResponseData.Result.SUCCESS,oceanengineAccount);
    }

    @Override
    public ResponseData getThirdSite(ThirdSiteRequest thirdSiteRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri ="tools/third_site/get/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());
        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(thirdSiteRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineGetThirdSite oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineGetThirdSite.class);
        return new ResponseData(ResponseData.Result.SUCCESS,oceanengineAccount);
    }
    @Override
    public OceanengineGetAdConvert getAdConvert(GetAdConvertRequest getAdConvertRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "tools/ad_convert/select/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());
        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getAdConvertRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineGetAdConvert oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineGetAdConvert.class);
        return oceanengineAccount;
    }

    @Override
    public OceanengineQueryAdConvert queryAdConvert(QueryAdConvertRequest getAdConvertRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "tools/ad_convert/query/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());
        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getAdConvertRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineQueryAdConvert oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineQueryAdConvert.class);
        return oceanengineAccount;
    }
}
